package ajax;

import Impl.TopicDaoImpl;
import Impl.plateDaoImpl;
import entity.Plate;
import entity.page.IndexPage;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

@WebServlet(name = "ShowBlockAjax", urlPatterns = "/showBlockAjax")
public class ShowBlockAjax extends HttpServlet {
    plateDaoImpl plateDao = new plateDaoImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        System.out.println("------正在执行ShowBlockAjax-------------");
        ArrayList<Plate> sectionInfoList;
        ArrayList<Plate> rootSectionInfoList;
        ArrayList<Plate> childSectionInfoList;
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");// a为am/pm的标记
        TopicDaoImpl topicDao = new TopicDaoImpl();

        sectionInfoList = (ArrayList<Plate>) plateDao.findAllPlate();
        rootSectionInfoList = (ArrayList<Plate>) plateDao.findPlateByPId(0);
        response.setCharacterEncoding("utf-8");


        String html = "";
        String htmlIn = "";
        for (int i = 0; i < rootSectionInfoList.size(); i++) {
            html += "<table cellSpacing='0' cellPadding='0' width='100%'> \n" +
                    "<tr class='tr2' align='center'> \n" +
                    "<td colSpan='2'>论坛</td> \n" +
                    "<td style='width: 10%'>主题</td> \n" +
                    "<td style='width: 30%'>最后发表</td> \n" +
                    "</tr> \n" +
                    "<tr class='tr3'> \n" +
                        "<td colspan='4'> \n" +
                            "<a href='/showList.jhtml?sid=" + rootSectionInfoList.get(i).getSid() +"'>" + rootSectionInfoList.get(i).getsName() + "</a>" +
                        "</td> \n" +
                    "</tr> \n";

            childSectionInfoList = (ArrayList<Plate>) plateDao.findPlateBySid(rootSectionInfoList.get(i).getSid());

            if (childSectionInfoList != null) {
                for (int j = 0; j < childSectionInfoList.size(); j++) {
                    html += "<tr class='tr3'> \n" +
                                "<td width='5%'>&nbsp;</td> \n" +
                                "<th align='left'><img src='img/head/1.gif'> \n" +
                                "<a href='/showList.jhtml?sid=" + childSectionInfoList.get(j).getSid() + "'>"+ childSectionInfoList.get(j).getsName() +"</a> \n" +
                                "</th> \n" +
                                "<td align='center'> \n" +
                                childSectionInfoList.get(j).getsTopicCount() +
                                "</td>\n" +
                                "<th> \n";

                    if (childSectionInfoList.get(j).getsTopicCount() != 0) {
                        IndexPage indexPageInfo = topicDao.getALLTopicLastTimeById(childSectionInfoList.get(j).getSid());
//                        if (indexPageInfo.getAuthor() != null) {
                            String latestTopic = indexPageInfo.getTtopic();
                            String latesetAuthor = indexPageInfo.getAuthor();
                            Integer tid = indexPageInfo.getTid();
                            Integer sid = childSectionInfoList.get(j).getSid();
                            String latestTime = sdf.format(indexPageInfo.getTpublishtime());


                            html +=
                                    "<span><a href='/showArticle.jhtml?sid=" + sid +"&tid=" + tid + "'>" + latestTopic + "</a></span><br/> \n" +
                                            "<span>" + latesetAuthor + "</span> \n" +
                                            "<span class='gray'>\n" + latestTime
                                            +"</span>\n" +
                                            "</th> \n" +
                                            "</tr>";
//                        }else {
//                            html += "<span>从未</span> \n" +
//                                    "</th> \n" +
//                                    "</tr>";
//                        }
                    } else {
                        html += "<span>从未</span> \n" +
                                "</th> \n" +
                                "</tr>";
                    }


                }
            }

        }
        html += "</table>";

        response.getWriter().write(html);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
